#!/bin/sh

# 测试 ZCARD
./redis-cli ZADD myzset 1 "one" 2 "two" 3 "three"
if [ $? -eq 0 ]; then
    echo "ZADD test succeeded"
else
    echo "ZADD test failed: error executing command"
    exit 1
fi

result=$(./redis-cli ZCARD myzset)
if [ $? -eq 0 ]; then
    if [ "$result" -eq 3 ]; then
        echo "ZCARD test succeeded: $result elements in myzset"
    else
        echo "ZCARD test failed: expected 3, got $result"
    fi
else
    echo "ZCARD test failed: error executing command"
fi

# 测试 HVALS
./redis-cli HSET myhash field1 "foo" field2 "bar"
if [ $? -eq 0 ]; then
    echo "HSET test succeeded"
else
    echo "HSET test failed: error executing command"
    exit 1
fi

result=$(./redis-cli HVALS myhash | tr '\n' ' ')
if [ $? -eq 0 ]; then
    expected_values="foo bar "
    if [ "$result" = "$expected_values" ]; then
        echo "HVALS test succeeded: values are $result"
    else
        echo "HVALS test failed: expected '$expected_values', got '$result'"
    fi
else
    echo "HVALS test failed: error executing command"
fi

# 测试 FLUSHALL
# 向数据库中添加一些数据
./redis-cli SET key1 "value1"
./redis-cli SET key2 "value2"

# 检查添加的数据是否存在
result1=$(./redis-cli GET key1)
result2=$(./redis-cli GET key2)

if [ "$result1" = "value1" ] && [ "$result2" = "value2" ]; then
    echo "Data added successfully"
else
    echo "Error adding data"
    exit 1
fi

# 使用 FLUSHALL 命令清空所有数据
./redis-cli FLUSHALL
if [ $? -eq 0 ]; then
    echo "FLUSHALL command executed successfully"
else
    echo "FLUSHALL command failed"
    exit 1
fi

# 检查数据是否被成功删除
result1=$(./redis-cli GET key1)
result2=$(./redis-cli GET key2)

if [ -z "$result1" ] && [ -z "$result2" ]; then
    echo "FLUSHALL test succeeded: all data removed"
else
    echo "FLUSHALL test failed: data still exists"
fi

# 测试 EVALSHA_RO
# 加载 Lua 脚本并获取 SHA1 散列值
SHA1=$(./redis-cli SCRIPT LOAD "return redis.call('get', KEYS[1])")
if [ $? -eq 0 ]; then
    echo "SCRIPT LOAD succeeded, SHA1: $SHA1"
else
    echo "SCRIPT LOAD failed"
    exit 1
fi

# 添加一个测试键值对
./redis-cli SET mykey "myvalue"
if [ $? -eq 0 ]; then
    echo "SET command succeeded"
else
    echo "SET command failed"
    exit 1
fi

# 使用 EVALSHA_RO 只读地执行脚本
result=$(./redis-cli EVALSHA_RO $SHA1 1 mykey)
if [ "$result" = "myvalue" ]; then
    echo "EVALSHA_RO test succeeded: $result"
else
    echo "EVALSHA_RO test failed: $result"
fi

# 测试 MULTI
# 使用 MULTI 开始事务
result=$(./redis-cli MULTI)
if [ "$result" = "OK" ]; then
    echo "MULTI command succeeded"
else
    echo "MULTI command failed"
    exit 1
fi

# 在事务中添加命令
./redis-cli SET key1 "value1"
if [ $? -eq 0 ]; then
    echo "SET key1 command succeeded"
else
    echo "SET key1 command failed"
    exit 1
fi

./redis-cli SET key2 "value2"
if [ $? -eq 0 ]; then
    echo "SET key2 command succeeded"
else
    echo "SET key2 command failed"
    exit 1
fi

# 提交事务
result=$(./redis-cli EXEC)
if [[ "$result" == *"1) OK"* && "$result" == *"2) OK"* ]]; then
    echo "EXEC command succeeded"
else
    echo "EXEC command failed"
    exit 1
fi

# 验证事务执行结果
result=$(./redis-cli GET key1)
if [ "$result" = "value1" ]; then
    echo "GET key1 command succeeded: $result"
else
    echo "GET key1 command failed: $result"
    exit 1
fi

result=$(./redis-cli GET key2)
if [ "$result" = "value2" ]; then
    echo "GET key2 command succeeded: $result"
else
    echo "GET key2 command failed: $result"
    exit 1
fi

# 测试 RPOPLPUSH
# 添加测试数据到列表 mylist
./redis-cli LPUSH mylist "one" "two" "three"
if [ $? -eq 0 ]; then
    echo "LPUSH mylist command succeeded"
else
    echo "LPUSH mylist command failed"
    exit 1
fi

# 使用 RPOPLPUSH 命令
result=$(./redis-cli RPOPLPUSH mylist myotherlist)
if [ "$result" = "one" ]; then
    echo "RPOPLPUSH command succeeded: $result"
else
    echo "RPOPLPUSH command failed: $result"
    exit 1
fi

# 验证 mylist 列表状态
result=$(./redis-cli LRANGE mylist 0 -1)
if [ "$result" = "three\ntwo" ]; then
    echo "LRANGE mylist command succeeded: $result"
else
    echo "LRANGE mylist command failed: $result"
    exit 1
fi

# 验证 myotherlist 列表状态
result=$(./redis-cli LRANGE myotherlist 0 -1)
if [ "$result" = "one" ]; then
    echo "LRANGE myotherlist command succeeded: $result"
else
    echo "LRANGE myotherlist command failed: $result"
    exit 1
fi

# 测试 SMOVE
# 添加测试数据到集合 myset
./redis-cli SADD myset "one" "two"
if [ $? -eq 0 ]; then
    echo "SADD myset command succeeded"
else
    echo "SADD myset command failed"
    exit 1
fi

# 添加测试数据到集合 myotherset
./redis-cli SADD myotherset "three"
if [ $? -eq 0 ]; then
    echo "SADD myotherset command succeeded"
else
    echo "SADD myotherset command failed"
    exit 1
fi

# 使用 SMOVE 命令将 "one" 从 myset 移动到 myotherset
result=$(./redis-cli SMOVE myset myotherset "one")
if [ "$result" = "1" ]; then
    echo "SMOVE command succeeded"
else
    echo "SMOVE command failed"
    exit 1
fi

# 验证 myset 集合状态
result=$(./redis-cli SMEMBERS myset)
if [ "$result" = "two" ]; then
    echo "SMEMBERS myset command succeeded: $result"
else
    echo "SMEMBERS myset command failed: $result"
    exit 1
fi

# 验证 myotherset 集合状态
result=$(./redis-cli SMEMBERS myotherset)
if [ "$result" = "three\ntwo" ]; then
    echo "SMEMBERS myotherset command succeeded: $result"
else
    echo "SMEMBERS myotherset command failed: $result"
    exit 1
fi

# SCRIPT HELP
./redis-cli SCRIPT HELP
if [ $? -eq 0 ]; then
    echo "SCRIPT HELP succeeded"
else
    echo "SCRIPT HELP error"
fi

# SCRIPT FLUSH
./redis-cli SCRIPT FLUSH
if [ $? -eq 0 ]; then
    echo "SCRIPT FLUSH succeeded"
else
    echo "SCRIPT FLUSH error"
fi

# SCRIPT DEBUG
./redis-cli SCRIPT DEBUG NO
if [ $? -eq 0 ]; then
    echo "SCRIPT DEBUG succeeded"
else
    echo "SCRIPT DEBUG error"
fi

# SCRIPT LOAD
SHA1=$(./redis-cli SCRIPT LOAD "return 'hello world'")
if [ $? -eq 0 ]; then
    echo "SCRIPT LOAD succeeded: $SHA1"
else
    echo "SCRIPT LOAD error"
fi

# SCRIPT EXISTS
./redis-cli SCRIPT EXISTS $SHA1
if [ $? -eq 0 ]; then
    echo "SCRIPT EXISTS succeeded"
else
    echo "SCRIPT EXISTS error"
fi

# SCRIPT KILL
./redis-cli SCRIPT KILL
if [ $? -eq 0 ]; then
    echo "SCRIPT KILL succeeded"
else
    echo "SCRIPT KILL error"
fi

# ZRANK
./redis-cli ZADD myzset 1 "one" 2 "two" 3 "three"
rank=$(./redis-cli ZRANK myzset "two")

if [ "$rank" = "1" ]; then
    echo "ZRANK succeeded"
else
    echo "ZRANK error"
fi

# SETBIT
./redis-cli SETBIT mykey 7 1
bit=$(./redis-cli GETBIT mykey 7)

if [ "$bit" = "1" ]; then
    echo "SETBIT succeeded"
else
    echo "SETBIT error"
fi

# GEODIST
./redis-cli GEOADD locations 13.361389 38.115556 "Palermo"
./redis-cli GEOADD locations 15.087269 37.502669 "Catania"
distance=$(./redis-cli GEODIST locations "Palermo" "Catania" km)

if [ -n "$distance" ]; then
    echo "GEODIST succeeded: $distance km"
else
    echo "GEODIST error"
fi

# BITCOUNT
./redis-cli SET mykey "foobar"
result=$(./redis-cli BITCOUNT mykey)

if [ "$result" -eq 26 ]; then
    echo "BITCOUNT succeeded: $result"
else
    echo "BITCOUNT error"
fi

# SINTERSTORE
./redis-cli SADD set1 "a" "b" "c"
./redis-cli SADD set2 "b" "c" "d"
./redis-cli SINTERSTORE resultset set1 set2
result=$(./redis-cli SMEMBERS resultset)

if [ "$result" = "b c" ]; then
    echo "SINTERSTORE succeeded: $result"
else
    echo "SINTERSTORE error"
fi

# DUMP
./redis-cli SET mykey "Hello"
dump_result=$(./redis-cli DUMP mykey)

if [ -n "$dump_result" ]; then
    echo "DUMP succeeded: $dump_result"
else
    echo "DUMP error"
fi

# XGROUP
# 创建流和消费者组
./redis-cli XADD mystream * key1 value1
./redis-cli XGROUP CREATE mystream mygroup $ && echo "XGROUP CREATE succeeded" || echo "XGROUP CREATE error"

# 读取消息
./redis-cli XREADGROUP GROUP mygroup consumer1 STREAMS mystream > && echo "XREADGROUP succeeded" || echo "XREADGROUP error"

# 删除消费者组
./redis-cli XGROUP DESTROY mystream mygroup && echo "XGROUP DESTROY succeeded" || echo "XGROUP DESTROY error"

# 脚本管理
if ./redis-cli SCRIPT LOAD "return 'hello world'" && ./redis-cli SCRIPT FLUSH; then
    echo "Script management test succeeded"
else
    echo "Script management test failed: error executing command"
fi
